package com.laiketui.plugin.common.mapper;

import com.laiketui.core.exception.LaiKeAPIException;
import com.laiketui.plugin.common.domain.base.flashsale.FlashsaleLabelModel;
import com.laiketui.plugin.common.tool.db.BaseMapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

public interface FlashsaleLabelModelMapper extends BaseMapper<FlashsaleLabelModel> {
    @Override
    List<Map<String, Object>> selectDynamic(Map<String, Object> map) throws LaiKeAPIException;

    @Override
    int countDynamic(Map<String, Object> map) throws LaiKeAPIException;

    /**
     * 查询当前时间段是否又进行中/未开始的活动
     * @param storeId
     * @param startTime
     * @param endTime
     * @param id
     * @return
     */
    @Select("select count(*) from lkt_flashsale_label where store_id = #{storeId} " +
            " and starttime <= #{endTime} and endtime >= #{startTime} " +
            " and mch_id = #{mchId} " +
            " and  `status` IN (1,2) and recovery = 0  and id != #{id}")
    Integer getLabelNumByStartTimeAndEndTimeNotId(Integer storeId, Integer mchId, String startTime, String endTime, Integer id);

    @Select("select count(*) from lkt_flashsale_label where store_id = #{storeId} " +
            " and starttime <= #{endTime} and endtime >= #{startTime} " +
            " and mch_id = #{mchId} " +
            " and  `status` IN (1,2) and recovery = 0 ")
    Integer getLabelNumByStartTimeAndEndTime(Integer storeId, Integer mchId, String startTime, String endTime);

    @Select(" select " +
            " b.id, b.discount, b.buylimit, b.id as fsGoodsId, b.goods_id as goodsId," +
            " p.num as fsMaxStockNum, p.product_title as goodsName, p.imgurl as imgUrl, a.status as fsStatus, a.endtime as remainingTime, " +
            " c.price as goodsPrice " +
            " from lkt_flashsale_label a " +
            " right join lkt_flashsale_activity b on a.id = b.label_id " +
            " inner join lkt_product_list p on b.goods_id = p.id " +
            " LEFT JOIN lkt_configure AS c ON p.id = c.pid" +
            " where a.store_id = #{storeId} and a.mch_id = #{mchId} and a.status = 2 and p.product_title like CONCAT('%',#{key},'%')" +
            " and recovery = 0" +
            " GROUP BY p.id")
    List<Map<String,Object>> getActivityListByMchId(Integer storeId, Integer mchId, String key);
}
